//
//  SEGCommonDoorListTableViewCell.m
//  SEGIntelligentHardware
//
//  Created by Clavis on 2019/5/5.
//  Copyright © 2019 Clavis. All rights reserved.
//

#import "SEGCommonDoorListTableViewCell.h"

@interface SEGCommonDoorListTableViewCell ()

@property (nonatomic, strong) UIView *container;
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *maskView;

@end

@implementation SEGCommonDoorListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.container = [[UIView alloc] init];
    self.container.layer.borderWidth = 1;
    self.container.layer.borderColor = kColor_L2.CGColor;
    self.container.layer.cornerRadius = 22;
    self.container.layer.masksToBounds = YES;
    [self.contentView addSubview:self.container];
    [self.container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(44);
    }];
    
    self.titleLabel = ({
        UILabel *label = [[UILabel alloc] init];
        [self.container addSubview:label];
        label.textColor = kColor_1;
        label.font = kFont_16;
        label;
    });
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 60, 0, -15));
    }];
    
    
    self.maskView = [[UIView alloc] init];
    self.maskView.backgroundColor = kColor_T;
    [self.container addSubview:self.maskView];
    
    self.icon = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_opendoor_key")];
        imageView.userInteractionEnabled = YES;
        imageView.backgroundColor = kColor_W;
        imageView.layer.cornerRadius = 21;
        imageView.layer.masksToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.backgroundColor = UIColor.orangeColor;
        [self.container addSubview:imageView];
        imageView;
    });
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(1);
        make.width.height.mas_equalTo(42);
        make.centerY.mas_offset(0);
    }];
    
    [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(1);
        make.height.equalTo(self.icon.mas_height);
        make.centerY.mas_offset(0);
        make.right.mas_equalTo(self.icon.mas_right).offset(-21);
    }];
    
    
    CGFloat __block source_x = 0;
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithActionBlock:^(UIPanGestureRecognizer * _Nonnull gesture) {
        
        switch (gesture.state) {
            case UIGestureRecognizerStateBegan: {
                source_x = self.icon.centerX;
                break;
            }
                
            case UIGestureRecognizerStateChanged: {
                CGPoint translation = [gesture translationInView:self.container];
                CGFloat destance = 0;
                if (translation.x <= 0) {
                    destance = MAX(source_x + translation.x, 1);
                } else {
                    destance = MIN(source_x + translation.x, LYScreenWidth() - 30 - 42);
                }
                [self.icon mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(destance);
                    make.width.height.mas_equalTo(42);
                    make.centerY.mas_offset(0);
                }];
                [self layoutIfNeeded];
                
                //NSLog(@"%lf, %lf, %lf, %lf", source_x, translation.x, destance, LYScreenWidth());
                break;
            }
                
            case UIGestureRecognizerStateEnded: {
                CGPoint translation = [gesture translationInView:self.container];
                //NSLog(@"translation.x = %lf", translation.x);
                if (translation.x + source_x + 21 >= (LYScreenWidth()-30)/2.0) {
                    if (self.openDoorBlock) {
                        self.openDoorBlock();
                    }
                }
                
                [self.icon mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_equalTo(1);
                    make.width.height.mas_equalTo(42);
                    make.centerY.mas_offset(0);
                }];
                [UIView animateWithDuration:0.2 animations:^{
                    [self layoutIfNeeded];
                }];
            }
                break;
            default:
                break;
        }
    }];
    [self.icon addGestureRecognizer:pan];
}

- (void)updateUIWithModel:(SEGNewMyDoorListModel *)doorModel {
    self.titleLabel.text = doorModel.name;
}

@end
